﻿using System;

class BitsExchange
{
    static void Main()
    {
        Console.WriteLine("A PROGRAM THAT EXCHANGES BITS 3, 4 AND 5 WITH BITS 24, 25 AND 26 OF A GIVEN 32-BIT UNSIGNED INTEGER.\r\n");
        Console.WriteLine("Enter an integer number:");
        string consoleInput = Console.ReadLine();
        uint number;
        while (!UInt32.TryParse(consoleInput, out number))
        {
            Console.WriteLine("Invalid value. Enter another number:");
            consoleInput = Console.ReadLine();
        }
        uint newNumber = number;
        int mask;
        uint numberAndMask;
        uint bitValueOne; // Value for bits 3, 4, 5
        uint bitValueTwo; // Value for bits 24, 25, 26
        int bitPositionOne; // Bit positions 3, 4, 5
        int bitPositionTwo; // Bit positions 24, 25, 26
 
        for (bitPositionOne = 3; bitPositionOne <= 5; bitPositionOne++)
        {
            //Extracting the values of bits 3, 4, 5
            mask = 1 << bitPositionOne;
            numberAndMask = Convert.ToUInt32(newNumber & mask);
            bitValueOne = numberAndMask >> bitPositionOne;

            //Extracting the values of bit 24, 25, 26
            bitPositionTwo = bitPositionOne + 21; // 21 is the difference between bits 24, 25, 26 andd bits 3, 4, 5
            mask = 1 << bitPositionTwo;
            numberAndMask = Convert.ToUInt32(newNumber & mask);
            bitValueTwo = numberAndMask >> bitPositionTwo;

            //Setting the values of bits 3, 4, 5 to the values of bits 24, 25, 26
            if (bitValueOne == 0)
            {
                //Seeting to 0
                mask = ~(1 << bitPositionTwo);
                newNumber = Convert.ToUInt32(newNumber & mask);
            }
            else
            {
                //Setting to 1
                mask = 1 << bitPositionTwo;
                newNumber = Convert.ToUInt32(newNumber | mask);
            }

            //Setting the values of bits 24, 25, 26 to the values of bits 3, 4, 5
            if (bitValueTwo == 0)
            {
                mask = ~(1 << bitPositionOne);
                newNumber = Convert.ToUInt32(newNumber & mask);
            }
            else
            {
                mask = 1 << bitPositionOne;
                newNumber = Convert.ToUInt32(newNumber | mask);
            }      
        }
        //Printing the result on the screen
        Console.WriteLine("\r\nThe result after exchanging bits 3/4/5 with bits 24/25/26 is:\r\n");
        Console.Write("Old number: ");
        Console.Write(Convert.ToString(number).PadLeft(11));
        Console.Write("  ");
        Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0'));
        Console.Write("New number: ");
        Console.Write(Convert.ToString(newNumber).PadLeft(11));
        Console.Write("  ");
        Console.WriteLine(Convert.ToString(newNumber, 2).PadLeft(32, '0'));
        Console.WriteLine();
    }
}